<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
use Home\Model\OrderList;
use Hdphp\Cart;
//测试控制器
class OrderListController extends Controller{
    private $model;
	//构造函数
	public function __init(){
        // parent::__init();
        $this->model = new OrderList;
	}
	
    //动作
    public function index(){
   
        $data = cart::getAllData();
        View::with('data',$data);
        View::make();
    }
    //ajax数据请求
    public function getAjax(){
        if(IS_AJAX){
                //获得gid
            $gid = Q('post.gid',0,'intval');
                //获得gaid
            $gaid = Q('post.gaid');
                //用户购买数量
            $num = Q('post.num',0,'intval');
            //转换 |  为逗号
            $gaidv = str_replace('|', ',', $gaid);
            $gaModel = new \Admin\Model\Goods_attr;
            $gaData = array();
                //获取所选货品的规格
            $gaData = $gaModel->where("goods_gid={$gid} and gaid in ($gaidv)")->get();
                //获得货品的glid
            $glidData = Db::table('goods_list')->where("combine='{$gaid}'")->first();

                //获取当前的价格
            $dqdata = Db::table("goods")->where("gid={$gid}")->get();
                //商品单价
            $price = $dqdata[0]['shop_price'];
                //获取规格
            $select_menu = Db::select("select taname,group_concat(gaid) as item_id,group_concat(gavalue) as item from goods_attr join type_attribute on type_attribute_taid=taid where goods_gid=$gid and class=2 group by taid ");
            $gaidd = array();
            foreach ($select_menu as $k => &$xxv) {
                $xxv['item'] = explode(',', $xxv['item']);
                $gaidd[] = explode(',', $xxv['item_id']);

            }
            foreach($select_menu as $k=>$vk){
                foreach($vk['item'] as $kk=>$vit){
                    $select_menu[$k]['item_son'][$gaidd[$k][$kk]] = $vit;
                }
            }
             //把得到的组合ID拆分开
            $gaid = explode('|', $gaid);
                    //获得每个规格的加价
            $option = array();
            foreach($gaid as $kg => $vd){
                $price +=$gaData[$kg]['value_added'];
                $option[$select_menu[$kg]['taname']] = $select_menu[$kg]['item_son'][$vd];
            }
            $data = array(
                'id'    =>$gid, //商品ID
                'name'  =>$dqdata[0]['gname'], //商品名称
                'num'    =>$num, //购买商品数量
                'price'  =>$price, //单价 
                'glid'   =>$glidData['glid'],
                'img'    =>$dqdata[0]['list_view'], //图品地址
                'options' =>$option,
            );
            cart::add($data);
            $goData = array(
                'gname'   => $dqdata[0]['gname'],
                'number'  => cart::getTotalNums(),
                'price'   => cart::getTotalPrice(),
            );
            if(!$goData){
              $goData = array();
            }
            echo json_encode($goData); 
            exit;




        }
    }

    //删除
    public function del(){
        $k = Q('get.k');
        Cart::del($k);
        go(U('index'));
    }
    //购物车商品数量加减
    public function addReduce(){
        if(IS_AJAX){
            $num = Q('post.num',0,'intval');
            $kk = Q('post.kkey');
            $data = array(
                'sid'    =>$kk, //商品ID
                'num'    =>$num, //购买商品数量
            );
            Cart::update($data);
            exit;
        }
    }
    
    // 验证订单
    public function check(){
        if(IS_AJAX){
                //实例化订单表
            $orderModel = new \Home\Model\Orders;
                //验证
            $oid = $orderModel->store();
            if($oid){
                $ListModel = new \Home\Model\OrderList;
                $ListModel->store($oid);
            }
            $_SESSION['oid'] = $oid;
            if(!$oid){
                echo json_encode(array('status'=>false,'message'=>$orderModel->getError()));
                exit;
            }else{
                cart::flush();
                echo json_encode(array('status'=>true,'message'=>'提交成功'));
                exit;
            }
        }
        //获得用户信息
        if(!isset($_SESSION['username']) || !isset($_SESSION['uid'])){
            $_SESSION['history'] = $_SERVER['HTTP_REFERER'];
            View::success('请先登陆',U('Login/index'));
            exit;
        }
        $uid = $_SESSION['uid'];
            //获取地址
        $address = Db::table('user_address')->where("user_uid={$uid}")->get();
        // sp($address);
        View::with('address',$address);
        View::make();
    }
    
    
	
}
?>